package Java_9_15zy;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Market implements Serializable {
    private String name;//超市名
    private String addess;//超市地址
    private String tel;//电话
    Map<String ,MarketVip> m=new HashMap<>(); //存会员
//无参构造
   /* public Market() {

    }*/
//有参构造
    public Market(String name, String addess, String tel) {
        this.name = name;
        this.addess = addess;
        this.tel = tel;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddess() {
        return addess;
    }

    public void setAddess(String addess) {
        this.addess = addess;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    @Override
    public String toString() {
        return "合家福超市{" +
                "名字='" + name + '\'' +
                ", 地址='" + addess + '\'' +
                ", 电话='" + tel + '\'' +
                '}';
    }
    //会员开卡
    public boolean addVip(MarketVip newvip){
        if (m.containsKey(newvip.getVipNumber())){
            System.out.println("卡号已经存在");
            return false;
        }else{
            m.put(newvip.getVipNumber(),newvip);
            System.out.println("开卡成功");
            return true;
        }
    }
    //会员登录
    public boolean dl(String vipNumber,String cipher){
        Set<Map.Entry<String, MarketVip>> entries = m.entrySet();
        for (Map.Entry x:entries){
            if (vipNumber.equals(x.getKey())&&cipher.equals(((MarketVip)(x.getValue())).getCipher())){
                System.out.println("登录成功");
                return true;
            }else{
                System.out.println("登录失败，账号或者密码错误");
                return false;
            }
        }

        return false;
    }

    //修改会员密码
     public boolean xg(String vipNumber , String cipher,String newCipher){
         Set<Map.Entry<String, MarketVip>> entries = m.entrySet();
         for (Map.Entry x:entries){
             if (vipNumber.equals(x.getKey())&&cipher.equals(((MarketVip)(x.getValue())).getCipher())){
                 ((MarketVip)(x.getValue())).setCipher(newCipher);
                 System.out.println("修改成功");
                 return true;
             }else{
                 System.out.println("修改失败");
                 return false;
             }
         }
         return false;
     }
     //退卡
     public boolean yc(String vipNumber,String cipher){
         Set<Map.Entry<String, MarketVip>> entries = m.entrySet();
         for (Map.Entry x:entries){
             if (vipNumber.equals(x.getKey())&&cipher.equals(((MarketVip)(x.getValue())).getCipher())){
                 m.remove(vipNumber);
                 System.out.println("退卡成功");
                 return true;
             }else{
                 System.out.println("退卡失败，您输入的账户密码错误");
                 return false;
             }
         }
         return false;
     }
     //查询积分
    public int cx(String vipNumber){
        Set<Map.Entry<String, MarketVip>> entries = m.entrySet();
        for (Map.Entry x:entries){
            if (vipNumber.equals(x.getKey())){
                int s = ((MarketVip)(x.getValue())).getCredit();
                System.out.println("积分是"+s);
                return s;
            }
        }
        return 0;
    }
    //积分累计
    public boolean lj(String vipNumber,int credit){
        Set<Map.Entry<String, MarketVip>> entries = m.entrySet();
        for (Map.Entry x:entries){
            if (vipNumber.equals(x.getKey())){
                int i = ((MarketVip) (x.getValue())).getCredit();
                ((MarketVip)(x.getValue())).setCredit(i+credit);
                System.out.println("添加成功");
                return true;
            }else{
                System.out.println("添加失败");
                return false;
            }
        }
        return false;
    }
    //积分兑换
    public String  dh(String vipNumber){
        Set<Map.Entry<String, MarketVip>> entries = m.entrySet();
        for (Map.Entry x:entries){
            if (vipNumber.equals(x.getKey())){
                int i = ((MarketVip) (x.getValue())).getCredit();
                if (i>=10000){
                    System.out.println("兑换了一个电饭锅");
                    ((MarketVip) (x.getValue())).setCredit(i-10000);
                    return "电饭锅";
                }else if (i>=5000){
                    System.out.println("兑换了一个炒锅");
                    ((MarketVip)(x.getValue())).setCredit(i-5000);
                    return "炒锅";
                }else{
                    System.out.println("积分太少，兑换失败");
                    return null;
                }
            }
        }
        return null;
    }
}
